package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-09-30
 * Time: 15:55
 * Description: 登陆注册密码输入框  暂时不对外设置子控件属性
 */
public class EditTextLayout extends LinearLayout implements View.OnClickListener, TextWatcher {

    private ImageView mIvDelete;
    private ImageView mIvVisible;
    private EditText mEtInput;

    private boolean isVisible = false;

    public EditTextLayout(Context context) {
        this(context, null);
    }

    public EditTextLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditTextLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.dl_edittext_layout, this);
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.password_et_style);
        String string = ta.getString(R.styleable.password_et_style_inputHint);
        float pixelSize = ta.getDimension(R.styleable.password_et_style_inputTextSize, 14);
        int color = ta.getColor(R.styleable.password_et_style_inputTextHintColor, Color.parseColor("#C3808080"));
        ta.recycle();
        initView(view);
        if (!TextUtils.isEmpty(string)) {
            mEtInput.setHint(string);
        }
        mEtInput.setHintTextColor(color);
        mEtInput.setTextSize(TypedValue.COMPLEX_UNIT_PX, pixelSize);
        bindAction();
        changeStatus(true);
        setGravity(Gravity.CENTER_VERTICAL);
    }

    public String getInputText() {
        return mEtInput.getText().toString().trim();
    }

    public void clearInput() {
        mEtInput.getText().clear();
    }

    public void setOnEditorActionListener(TextView.OnEditorActionListener listener) {
        mEtInput.setOnEditorActionListener(listener);
    }

    public void setOnFocusChangeListener(OnFocusChangeListener listener) {
        mEtInput.setOnFocusChangeListener(listener);
    }

    private void changeStatus(boolean isDefault) {
        if (isDefault) {
            mIvDelete.setVisibility(GONE);
            mIvVisible.setVisibility(VISIBLE);
        } else {
            mIvVisible.setVisibility(VISIBLE);
            mIvDelete.setVisibility(VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_delete) {
            mEtInput.getText().clear();
        } else if (v.getId() == R.id.iv_visible) {
            if (!isVisible) {
                mIvVisible.setImageResource(R.drawable.pass_word_show);
                // 明文显示
                mEtInput.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                isVisible = true;
            } else {
                mIvVisible.setImageResource(R.drawable.pass_word_hide);
                // 密文显示
                mEtInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                isVisible = false;
            }
            mEtInput.setSelection(mEtInput.getText().length());
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String res = s.toString().trim();
        if (!TextUtils.isEmpty(res)) {
            changeStatus(false);
        } else {
            changeStatus(true);
        }
    }

    private void bindAction() {
        mIvDelete.setOnClickListener(this);
        mIvVisible.setOnClickListener(this);
        mEtInput.addTextChangedListener(this);
    }

    private void initView(View view) {
        mIvDelete = (ImageView) view.findViewById(R.id.iv_delete);
        mIvVisible = (ImageView) view.findViewById(R.id.iv_visible);
        mEtInput = (EditText) view.findViewById(R.id.et_input);
    }

    @Override
    protected void onDetachedFromWindow() {
        mEtInput.removeTextChangedListener(this);
        super.onDetachedFromWindow();
    }
}
